package com.itheima.health.controller.idem;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {
    @Autowired
    private OrderSettingService orderSettingService;


    @GetMapping("/getOrderSettingByMonth")
    public Result findPage(Integer year, Integer month) {
        log.info("预约信息查询");
        List<OrderSettingParam> orderSettingParmList = orderSettingService.findPages(year, month);
        return new Result(true, MessageConst.GET_ORDERSETTING_SUCCESS, orderSettingParmList);
    }

    @PostMapping("/editNumberByDate")
    public Result add(@RequestBody OrderSetting orderSetting) {
        log.info("设置预约人数");
        orderSettingService.add(orderSetting);
        return new Result(true, MessageConst.ORDERSETTING_SUCCESS);
    }

    @SneakyThrows
    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        log.info("[预约-批量设置预约信息]");
        try {
            InputStream in = excelFile.getInputStream();
            XSSFWorkbook excel = new XSSFWorkbook(in);
            //获取Excel文件的第1个Sheet页
            XSSFSheet sheet = excel.getSheetAt(0);

            //获取Sheet页中,有数据的最后一行的行号
            int lastRowNum = sheet.getLastRowNum();

            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (int i = 1; i <= lastRowNum; i++) {
                //获取Sheet页中的行
                XSSFRow titleRow = sheet.getRow(i);
                //获取Sheet页中的列
                //获取行的第1个单元格
                XSSFCell cell1 = titleRow.getCell(0);
                if (cell1 != null) {
                    //获取单元格中的文本内容 日期格式
                    Date dateCellValue1 = cell1.getDateCellValue();
                    //获取行的第2个单元格
                    XSSFCell cell2 = titleRow.getCell(1);
                    if (cell2 != null) {
                        //获取单元格中的文本内容 数值格式
                        double numericCellValue = cell2.getNumericCellValue();
                        OrderSetting orderSetting = new OrderSetting();
                        orderSetting.setOrderDate(dateCellValue1);
                        orderSetting.setNumber((int) numericCellValue);
                        orderSettingList.add(orderSetting);

                    }else {
                        //如果两个单元格中有一格为空值，则抛出异常，上传失败!
                        return new Result(false, MessageConst.FAFASASFASF_ASFASFASFA);
                    }

                }else {
                    return new Result(false, MessageConst.FAFASASFASF_ASFASFASFA);
                }
            }
            //批量插入数据库中
            orderSettingService.batchAdd(orderSettingList);
            //关闭资源
            in.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(true, MessageConst.UPLOAD_SUCCESS);
    }
}